package com.clwl.supervise.superviseapp.controller.inspection;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clwl.supervise.superviseapp.config.RepeatSubmit;
import com.clwl.supervise.superviseapp.entity.base.CommonSearchEntity;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.supervise.JgDailyCheck;
import com.clwl.supervise.superviseapp.service.inspect.SpecialInspectService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 *  专项检查
 */
@Slf4j
@RestController
@RequestMapping("specialInspect")
public class SpecialInspectController {



    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SpecialInspectService specialInspectService;

    /**
     * 获取专项检查的企业列表
     * @param pageNum 当前页数
     * @param pageSize 每页数据数量
     * @return
     */
    @GetMapping("getList")
    public ResultEntity<IPage<Map<String,Object>>> getList(Integer pageNum, Integer pageSize, CommonSearchEntity params
    ) {
        IPage<Map<String,Object>> pageList = specialInspectService.selectCompanyPage(pageNum,pageSize,params);
        return ResultEntity.success(pageList);
    }


    /**
     * 获取检查的时候基础信息
     * @param unplannedCompanyId JG_UNPLANNED_COMPANY表ID
     * @return
     */
    @GetMapping("getBaseInfo")
    public ResultEntity<JgDailyCheck> getBaseInfo(Long unplannedCompanyId){
        try {
            JgDailyCheck checkInfo = specialInspectService.getSpecialCheckInfo(unplannedCompanyId);
            return ResultEntity.success(checkInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error(e.getMessage());
        }
    }



    /**
     * 保存/临时保存检查项结果内容
     * @param temporary  是否临时1-是；2-否
     * @return
     */
    @PostMapping("saveCheckInfo")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity saveCheckInfo(  String temporary,  String checkData ){
        try {
            JgDailyCheck dailyCheck = JSONObject.parseObject(checkData, JgDailyCheck.class);
            JgDailyCheck dailyCheckResult =  specialInspectService.saveCheckInfo(temporary,dailyCheck);
            return ResultEntity.success(dailyCheckResult);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存/临时保存检查项结果内容失败");
            return ResultEntity.error("操作失败");
        }
    }

    /**
     * 最终保存
     * @param checkId
     * @return
     */
    @PostMapping("finaSaveInfo")
    @RepeatSubmit(lockTime = 10)
    public ResultEntity finaSaveInfo(Long checkId){
        try {
            JgDailyCheck dailyCheck =  specialInspectService.finaSaveInfo(checkId);
            return ResultEntity.success(dailyCheck);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("保存/临时保存检查项结果内容失败");
            return ResultEntity.error("操作失败");
        }
    }


}
